package com.mall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.entity.SmsCoupon;
import com.mall.entity.SmsCouponHistory;
import com.mall.mapper.SmsCouponHistoryMapper;
import com.mall.mapper.SmsCouponMapper;
import com.mall.service.SmsCouponService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon> implements SmsCouponService {

    @Resource
    private SmsCouponMapper smsCouponMapper;

    @Resource
    private SmsCouponHistoryMapper smsCouponHistoryMapper;

    @Override
    public SmsCoupon getById(Integer id) {
        return smsCouponMapper.selectById(id);
    }

    @Override
    public List<SmsCoupon> getAll() {
        return smsCouponMapper.selectList(null);
    }

    @Override
    public boolean save(SmsCoupon smsCoupon) {
        if(smsCoupon.getId() == null)
        {
            return smsCouponMapper.insert(smsCoupon) > 0;
        }else{
            return  smsCouponMapper.updateById(smsCoupon) > 0;
        }
    }

    @Override
    public boolean delete(Integer id) {
        return smsCouponMapper.deleteById(id) > 0;
    }

    @Override
    public List<SmsCoupon> getAbleCoupon(Integer memberId) {
        int receiveNum = 0;
        List<SmsCoupon> list = smsCouponMapper.getAbleCoupon();
        List<SmsCoupon> result = new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            receiveNum = smsCouponHistoryMapper.getCounts(memberId,list.get(i).getId());
            if(receiveNum < list.get(i).getPerLimit())
            {
                result.add(list.get(i));
            }
        }
        return result ;
    }
}
